package com.ruoyi.parking.service.impl;

import java.util.List;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.parking.mapper.ParkingLotMapper;
import com.ruoyi.parking.mapper.ParkingSpaceMapper;
import com.ruoyi.parking.domain.ParkingLot;
import com.ruoyi.parking.domain.ParkingSpace;
import com.ruoyi.parking.service.IParkingSpaceService;

/**
 * 车位管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-27
 */
@Service
public class ParkingSpaceServiceImpl implements IParkingSpaceService 
{
    @Autowired
    private ParkingSpaceMapper parkingSpaceMapper;

    @Autowired
    private ParkingLotMapper parkingLotMapper;

    /**
     * 查询车位管理
     * 
     * @param id 车位管理主键
     * @return 车位管理
     */
    @Override
    public ParkingSpace selectParkingSpaceById(Long id)
    {
        return parkingSpaceMapper.selectParkingSpaceById(id);
    }

    /**
     * 查询车位管理列表
     * 
     * @param parkingSpace 车位管理
     * @return 车位管理
     */
    @Override
    public List<ParkingSpace> selectParkingSpaceList(ParkingSpace parkingSpace)
    {
        return parkingSpaceMapper.selectParkingSpaceList(parkingSpace);
    }

    /**
     * 新增车位管理
     * 
     * @param parkingSpace 车位管理
     * @return 结果
     */
    @Override
    public int insertParkingSpace(ParkingSpace parkingSpace)
    {
        // 校验停车场是否存在
        ParkingLot lot = parkingLotMapper.selectParkingLotById(parkingSpace.getLotId());
        if (lot == null)
        {
            throw new ServiceException("停车场不存在");
        }

        // 校验车位编号在同一停车场内是否唯一
        int count = parkingSpaceMapper.countByLotAndCode(parkingSpace.getLotId(), parkingSpace.getSpaceCode(), null);
        if (count > 0)
        {
            throw new ServiceException("车位编号在同一停车场内已存在");
        }

        parkingSpace.setCreateBy(SecurityUtils.getUsername());
        parkingSpace.setCreateTime(DateUtils.getNowDate());
        return parkingSpaceMapper.insertParkingSpace(parkingSpace);
    }

    /**
     * 修改车位管理
     * 
     * @param parkingSpace 车位管理
     * @return 结果
     */
    @Override
    public int updateParkingSpace(ParkingSpace parkingSpace)
    {
        // 校验停车场是否存在
        if (parkingSpace.getLotId() != null)
        {
            ParkingLot lot = parkingLotMapper.selectParkingLotById(parkingSpace.getLotId());
            if (lot == null)
            {
                throw new ServiceException("停车场不存在");
            }
        }

        // 校验车位编号在同一停车场内是否唯一（排除自己）
        if (parkingSpace.getSpaceCode() != null)
        {
            int count = parkingSpaceMapper.countByLotAndCode(parkingSpace.getLotId(), parkingSpace.getSpaceCode(), parkingSpace.getId());
            if (count > 0)
            {
                throw new ServiceException("车位编号在同一停车场内已存在");
            }
        }

        parkingSpace.setUpdateBy(SecurityUtils.getUsername());
        parkingSpace.setUpdateTime(DateUtils.getNowDate());
        return parkingSpaceMapper.updateParkingSpace(parkingSpace);
    }

    /**
     * 批量删除车位管理
     * 
     * @param ids 需要删除的车位管理主键
     * @return 结果
     */
    @Override
    public int deleteParkingSpaceByIds(Long[] ids)
    {
        return parkingSpaceMapper.deleteParkingSpaceByIds(ids);
    }

    /**
     * 删除车位管理信息
     * 
     * @param id 车位管理主键
     * @return 结果
     */
    @Override
    public int deleteParkingSpaceById(Long id)
    {
        return parkingSpaceMapper.deleteParkingSpaceById(id);
    }
}
